package com.example.edu.Mapper;

import com.example.edu.Dto.CourseListDtoR;
import com.example.edu.Entity.Course;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CourseMapper {
    @Insert("insert into course(course_name, course_teacher, invite_code, create_time) " +
            "VALUES (#{course.course_name},#{course.course_teacher},#{course.invite_code},unix_timestamp(now()))")
    @Options(useGeneratedKeys = true, keyProperty = "course_id") // 自动获取主键
    void addCourse(@Param("course") Course course);

    @Select("select count(*) from course where course_teacher = #{course_teacher}")
    int getCourseNum(@Param("course_teacher") int course_teacher);

    @Select("select course_id,course_name,invite_code,create_time from course where course_teacher = #{course_teacher} order by course_id " +
            "limit #{count} offset #{start}")
    List<CourseListDtoR> getCourses(@Param("course_teacher") int course_teacher,@Param("start") int start,@Param("count") int count);

    @Select("select count(*) from course_stu_course_user where user_id = #{user_id}")
    int getCourseNum2(@Param("user_id") int user_id);

    @Select("select course_id,course_name,invite_code,create_time,course_teacher from course_stu_course_user where user_id = #{user_id} " +
            "order by course_id limit #{count} offset #{start}")
    List<CourseListDtoR> getCourses2(@Param("user_id") int user_id,@Param("start") int start,@Param("count") int count);

    @Select("select course_name from course where course_id = #{course_id}")
    String getCourseName(@Param("course_id") int course_id);

    @Select("select course_name from course where course_id = #{course_id}")
    Course getCourse(@Param("course_id") int course_id);

    @Select("select course_id from course where invite_code = #{invite_code}")
    Integer getCourseIdByCode(@Param("invite_code") String invite_code);

    @Delete("delete from course where course_id = #{course_id}")
    void delete(@Param("course_id") int course_id);

    @Update("update course set course_name = #{course.course_name} , create_time = #{course.create_time} where course_id = #{course.course_id}")
    void update(@Param("course") Course course);
}
